<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 支付方式控制器
 * 
 * @author Ringo Chan<334258471@qq.com>
 */
class PaymentController extends AdminController{
    
    /**
     * 支付方式列表
     * 
     * @author Ringo Chan<334258471@qq.com>
     */
    public function index(){
        $M_payment = M('payment');
        
        $installs = $M_payment->select();
        
        foreach ($installs as $key => $item){
            $installs[$item['code']] = $item;
            unset($installs[$key]);
        }
        
        $files = glob(VENDOR_PATH . 'payment/*.php');
        
        $list = array();
        
        foreach ($files as $file) {
            $extension = basename($file, '.php');
            vendor("payment.".$extension);
            
            $class = new $extension();

            $list[] = array(
                'code' => $class::_CODE_,
                'title' => isset($installs[$class::_CODE_]) ? $installs[$class::_CODE_]['title'] : $class::_TITLE_,
                'website' => $class::_WEBSITE_,
                'version' => $class::_VERSION_,
                'fee' => $class::_FEE_,
                'sort' => isset($installs[$class::_CODE_]) ? $installs[$class::_CODE_]['sort'] : 0,
                'installed' => isset($installs[$class::_CODE_]) ? 1 : 0
            );
        }
        
        $this->meta_title = '支付方式列表';
        $this->assign('_list', $list);
        $this->display();
    }
    
    /**
     * 编辑支付方式
     * 
     * @param string $code
     * @author Ringo Chan<334258471@qq.com>
     */
    public function edit($code = ''){
        !$code && $this->error('参数不能为空');
        
        !vendor("payment.".$code) && $this->error('非法参数');
        
        $M_payment = M('payment');
        
        $info = $M_payment->where(array('code'=>$code))->find();
        
        !$info && $this->error('非法参数');
        
        if (IS_POST) {
            $title = I('post.title');
            $sort = I('post.sort');
            
            !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', $title) && $this->error('支付方式名称必须在2 - 50个字符之间，首字符必须为中文');
            !is_numeric($sort) && $sort < 0 && $this->error('排序必须为正整数');
            
            $status = $M_payment->where(array('code'=>$code))->save(array('title'=>$title, 'sort'=>$sort, 'update_time'=>NOW_TIME));
            
            $status ? $this->success('操作成功', U('Payment/index')) : $this->error('操作失败，未知错误');
        } else {
            $this->meta_title = '编辑支付方式';
            $this->assign('info', $info);
            $this->display();
        }
    }
    
    /**
     * 安装支付方式
     * @param string $code
     * 
     * @author Ringo Chan<334258471@qq.com>
     */
    public function install($code = ''){
        !$code && $this->error('参数不能为空'); 
        
        !vendor("payment.".$code) && $this->error('非法参数');
        
        $M_payment = M('payment');
        
        $info = $M_payment->where(array('code'=>$code))->find();
        
        $info && $this->error('已安装，请勿重复安装');
        
        $class = new $code();
        
        $installed = $M_payment->add(array('code'=>$class::_CODE_, 'title'=>$class::_TITLE_, 'create_time'=>NOW_TIME, 'update_time'=>NOW_TIME));
        
        $installed ? $this->success('安装成功') : $this->error('安装失败，未知错误');
    }
    
    /**
     * 卸载支付方式
     * 
     * @param string $code
     * @author Ringo Chan<334258471@qq.com>
     */
    public function uninstall($code = ''){
        !$code && $this->error('参数不能为空');
        
        !vendor("payment.".$code) && $this->error('非法参数');
        
        $M_payment = M('payment');
        
        $M_payment->where(array('code'=>$code))->delete() ? $this->success('卸载成功') : $this->error('卸载失败，未知错误');
    }
}
?>